import { Injectable } from '@nestjs/common';
import Redis from 'ioredis';

@Injectable()
export class RedisService {
  private readonly redisClient: Redis;

  constructor() {
    this.redisClient = new Redis({
      host: 'localhost', // Redis服务器地址
      port: 6379,        // Redis服务器端口，默认是6379
      // 如果需要密码，请取消注释下面一行并替换为你的密码
      // password: 'your_redis_password',
    });
    
    // 监听连接事件，以便确认连接是否成功
    this.redisClient.on('connect', () => {
      console.log('Connected to Redis successfully');
    });

    // 错误处理
    this.redisClient.on('error', (err) => {
      console.error('Redis connection error:', err);
    });
  }

  getClient(): Redis {
    return this.redisClient;
  }
}